﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
using AgentObjects;
using SimpleUtils;
using SimpleUtils.Win;

namespace SimpleReminder
{
    internal class Agent
    {
        public const int BalloonOn = 1;
        public const int BalloonAutoSize = 2;
        public const int BalloonAutoHide = 4;
        public const int BalloonAutoPace = 8;

        public const int REQUEST_COMPLETED = 0;
        public const int REQUEST_FAILED = 1;
        public const int REQUEST_PENDING = 2;
        public const int REQUEST_INTERRUPTED = 3;
        public const int REQUEST_INPROGRESS = 4;

        public const string RANDOM = "<RANDOM>";

        public static string GetRandomAgent()
        {
            string[] files = GetAgents();
            string f = null;
            if (files != null && files.Length > 0)
            {
                if (files.Length == 1)
                {
                    f = files[0];
                }
                else
                {
                    int r = Utils.GetRandomNo(files.Length);
                    f = files[r];
                }
            }
            return f;
        }

        public static string GetRandomAnimation(IAgentCtlCharacterEx character)
        {
            if (character == null) return null;
            List<string> list = new List<string>();
            foreach (string aniName in character.AnimationNames)
            {
                list.Add(aniName);
            }
            int r = Utils.GetRandomNo(list.Count);
            return list[r];
        }

        private static void FindAgents(string path, List<string> list)
        {
            string pattern = "*.acs";
            string[] files = Directory.GetFiles(path, pattern);
            if (files != null && files.Length > 0)
            {
                foreach (string f in files)
                {
                    list.Add(f);
                }
            }
        }
             
        public static string[] GetAgents()
        {
            List<string> list = new List<string>();
            
            //find in windows dir
            string path = Path.Combine(
                Path.GetDirectoryName(Environment.SystemDirectory),
                @"msagent\chars");
            FindAgents(path, list);

            //find in office dir
            for (int i = 10; i <= 12; i++)
            {
                path = String.Format(@"Software\Microsoft\Office\{0}.0\Common\InstallRoot", i);
                RegistryKey key = Registry.LocalMachine.OpenSubKey(path);
                if (key != null)
                {
                    path = key.GetValue("Path") as string;
                    if (path != null && Directory.Exists(path))
                    {
                        FindAgents(path, list);
                    }
                }
            }

            return list.ToArray();
        }

        public static void InitAgentList(ComboBox cboAgent)
        {
            cboAgent.Items.Clear();
            cboAgent.DisplayMember = "Text";
            cboAgent.ValueMember = "Value";

            string[] files = GetAgents();
            if (files != null && files.Length > 0)
            {
                Hashtable ht = new Hashtable();
                foreach (string f in files)
                {
                    string fileName = Path.GetFileName(f.ToUpper());
                    if (ht.ContainsKey(fileName)) continue;

                    SimpleListItem li = new SimpleListItem(f, fileName);
                    cboAgent.Items.Add(li);
                    ht.Add(fileName, 1);
                }
            }
        }

        public static void SetDefaultBallonStyle(IAgentCtlCharacterEx character)
        {
            character.Balloon.Style |= Agent.BalloonOn | Agent.BalloonAutoSize | Agent.BalloonAutoPace;
            character.Balloon.Style &= unchecked((int)(Agent.BalloonAutoHide ^ 0xFFFFFFFF));
        }

        public static void PlayAnimation(IAgentCtlCharacterEx character, string animation)
        {
            if (character == null || String.IsNullOrEmpty(animation)) return;

            IAgentCtlRequest req = character.Play(animation);

            DateTime startTime = DateTime.Now;
            while (req.Status == Agent.REQUEST_PENDING || req.Status == Agent.REQUEST_INPROGRESS)
            {
                Application.DoEvents();
                TimeSpan ts = DateTime.Now - startTime;
                if (ts.Seconds > 3)
                {
                    StopCharacter(character);
                    break;
                }
            }
        }

        public static void PlayAnimation(IAgentCtlCharacterEx character, IList animations)
        {
            if (character == null || animations == null || animations == null || animations.Count < 1) 
                return;

            StopCharacter(character);

            for (int i = 0; i < animations.Count; i++)
            {
                string ani = animations[i] as string;
                PlayAnimation(character, ani);
            }
        }

        public static bool StopCharacter(IAgentCtlCharacterEx character)
        {
            bool ok = true;
            if (character != null)
            {
                try
                {
                    character.Stop(null);
                }
                catch
                {
                    ok = false;
                }
            }
            return ok;
        }

        public static bool HideCharacter(IAgentCtlCharacterEx character)
        {
            bool ok = true;
            if (character != null)
            {
                try
                {
                    character.Hide(null);
                }
                catch
                {
                    ok = false;
                }
            }
            return ok;
        }

    }//end of class
}
